package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysUserFeedback;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysAddressRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysAddressMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysAddress;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysAddressService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地址设置业务层处理
 *
 * @author mars
 * @date 2024-04-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysAddressServiceImpl implements ISysAddressService {

    private final SysAddressMapper baseMapper;
    private final ISysDictDataService dataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysAddress add(SysAddressRequest request) {
        SysAddress entity = SysAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysAddressRequest request) {
        SysAddress entity = SysAddress.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysAddress getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysAddress> pageList(SysAddressRequest request) {
        Page<SysAddress> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysAddress> query = this.buildWrapper(request);
        IPage<SysAddress> pageRecord = baseMapper.selectPage(page, query);
        List<SysAddress> records = pageRecord.getRecords();
        List<SysAddress> recordsNew = new ArrayList<>();
        for (SysAddress address : records) {
            address.setTypeName(dataService.getDictDataById("address_type", address.getType().toString()));
            address.setSexName(dataService.getDictDataById("sex", address.getType().toString()));
            recordsNew.add(address);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysAddress> list(SysAddressRequest request) {
        LambdaQueryWrapper<SysAddress> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysAddress> buildWrapper(SysAddressRequest param) {
        LambdaQueryWrapper<SysAddress> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysAddress::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            query.like(SysAddress::getPhone, param.getPhone());
        }
        if (StringUtils.isNotBlank(param.getAddress())) {
            query.like(SysAddress::getAddress, param.getAddress());
        }
        if (param.getType() != null) {
            query.eq(SysAddress::getType, param.getType());
        }
        return query;
    }

}
